package com.sky.sky_server.controller.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.ShoppingCartDTO;
import com.sky.sky_pojo.entity.ShoppingCart;
import com.sky.sky_server.service.impl.ShoppingCartServiceImpl;

import lombok.extern.slf4j.Slf4j;

@RestController("userShoppingCartController")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {
    
    @Autowired
    private ShoppingCartServiceImpl shoppingCartServiceImpl;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加购物车：{}", shoppingCartDTO);
        int res = shoppingCartServiceImpl.addShoppingCart(shoppingCartDTO);
        if(res >= 0)
            return Result.success("添加成功");
        return Result.error("添加失败");
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查看购物车");
        List<ShoppingCart> list = shoppingCartServiceImpl.list();
        return Result.success(list);
    }

    /**
     * 删除购物车内的一个商品
     * @param id
     * @return
     */
    @PostMapping("/sub")
    public Result delete(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车：{}", shoppingCartDTO);
        int res = shoppingCartServiceImpl.delete(shoppingCartDTO);
        if(res >= 0)
            return Result.success("删除成功");
        return Result.error("删除失败");
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public Result clean() {
        log.info("清空购物车");
        shoppingCartServiceImpl.clean();
        return Result.success("清空成功");
    }


}
